<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>自动生成代码</title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <!-- Bootstrap 3.3.7 -->
  <link rel="stylesheet" th:href="@{/static/bootstrap/bootstrap.min.css}">
  <!-- Font Awesome -->
  <link rel="stylesheet" th:href="@{/static/css/font-awesome.min.css}">
  <!-- Ionicons -->
  <link rel="stylesheet" th:href="@{/static/css/ionicons.min.css}">
  <!-- Theme style -->
  <link rel="stylesheet" th:href="@{/static/adminlte/AdminLTE.min.css}">
  <!-- AdminLTE Skins. Choose a skin from the css/skins
       folder instead of downloading all of them to reduce the load. -->
  <link rel="stylesheet" th:href="@{/static/css/_all-skins.min.css}">

  <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->

  <!-- Google Font -->
  <link rel="stylesheet" th:href="@{/static/css/google-apis-fonts.css}">

  <!-- jQuery 3 -->
  <script th:src="@{/static/jQuery/jquery.min.js}"></script>
  <!-- Bootstrap 3.3.7 -->
  <script th:src="@{/static/bootstrap/bootstrap.min.js}"></script>
  <!-- SlimScroll -->
  <script th:src="@{/static/jQuery/jquery.slimscroll.min.js}"></script>
  <!-- FastClick -->
  <script th:src="@{/static/js/fastclick.js}"></script>
  <!-- AdminLTE App -->
  <script th:src="@{/static/adminlte/adminlte.min.js}"></script>
  <!-- AdminLTE for demo purposes -->
  <script th:src="@{/static/adminlte/demo.js}"></script>
  <script>
      $(document).ready(function () {
          $('.sidebar-menu').tree()
      })
  </script>

</head>
<body class="hold-transition skin-blue sidebar-mini">
<!-- Site wrapper -->
<div class="wrapper">

  <header class="main-header">
    <!-- Logo -->
    <a href="#" class="logo">
      <!-- mini logo for sidebar mini 50x50 pixels -->
      <span class="logo-mini"><b>Auto</b></span>
      <!-- logo for regular state and mobile devices -->
      <span class="logo-lg"><b>Auto</b>Code Generator</span>
    </a>
    <!-- Header Navbar: style can be found in header.less -->
    <nav class="navbar navbar-static-top">
      <!-- Sidebar toggle button-->
      <a href="#" class="sidebar-toggle" data-toggle="push-menu" role="button">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </a>
    </nav>
  </header>

  <!-- =============================================== -->

  <!-- Left side column. contains the sidebar -->
  <aside class="main-sidebar">
    <!-- sidebar: style can be found in sidebar.less -->
    <section class="sidebar">
      <!-- Sidebar user panel -->
      <div class="user-panel">
        <div class="pull-left image">
          <img th:src="@{/static/images/head-img.jpeg}" class="img-circle" alt="User Image">
        </div>
        <div class="pull-left info">
          <p>云弧代码生成器</p>
          <a href="#"><i class="fa fa-circle text-success"></i> code generator</a>
        </div>
      </div>
      <!-- sidebar menu: : style can be found in sidebar.less -->
      <ul class="sidebar-menu" data-widget="tree">
        <li class="header">code generator</li>
        <li><a href="/"><i class="fa fa-th"></i> <span>查看数据表列表</span></a></li>
      </ul>
    </section>
    <!-- /.sidebar -->
  </aside>

  <!-- =============================================== -->

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">

    <!-- Main content -->
    <section class="content">

      <!-- Default box -->
      <div class="box">
        <div class="box-body">
          <div class="form-inline">
            <div class="form-group">
              <label for="disk">选择盘符</label>
              <select name="disk" id="disk" class="form-control">
                <option th:value="*{disk.key}" th:each="disk : ${diskMap}" th:text="*{disk.value}"></option>
              </select>
            </div>
            <div class="form-group">
              <label for="destDir">目标目录</label>
              <input type="text" class="form-control" id="destDir" name="destDir" placeholder="目标目录">
            </div>
            <div class="form-group">
              <label for="projectName">项目名</label>
              <input type="text" class="form-control" id="projectName" name="projectName" placeholder="项目名">
            </div>
            <div class="form-group">
              <label for="basePackage">基础包结构</label>
              <input type="text" class="form-control" id="basePackage" name="basePackage" placeholder="基础包结构">
            </div>
            <button type="button" onclick="generate()" class="btn btn-default">生成项目</button>
            <form id="theForm" th:action="${#request.getContextPath()} + '/download'" method="post">
              <input hidden="hidden" name="reqStr" id="reqStr"/>
              <button type="button" onclick="generateAndDownload()" class="btn btn-default">生成项目并下载代码</button>
            </form>
          </div>

          <table class="table table-striped">
            <thead>
              <th><input type="checkbox" th:id="checkbox" name="checkbox" />&nbsp;&nbsp;选择</th>
              <th>序号</th>
              <th>表名</th>
              <th>表注释</th>
              <th>创建时间</th>
              <th>更新时间</th>
              <th>模块(包名)</th>
            </thead>
            <tbody>
              <tr th:each="table : ${tables}">
                <td><input type="checkbox" th:onclick="'doSelect(\'' + ${table.tableName} + '\')'" th:id="${table.tableName} + '_cid'" th:data-value="${table.tableName}" name="tableName"/></td>
                <td th:text="*{tableStat.index + 1}"></td>
                <td th:text="*{table.tableName}"></td>
                <td th:text="*{table.comment}"></td>
                <td th:text="*{#dates.format(table.createTime,'YYYY-MM-dd HH:mm:ss')}"></td>
                <td th:text="*{#dates.format(table.updateTime,'YYYY-MM-dd HH:mm:ss')}"></td>
                <td><input th:id="${table.tableName} + '_id'" th:onblur="'javascript:ensurePackageName(\'' + ${table.tableName} + '\')'"/></td>
              </tr>
            </tbody>
          </table>
        </div>
        <!-- /.box-body -->
      </div>
      <!-- /.box -->

    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->

  <footer class="main-footer">
    <div class="pull-right hidden-xs">
      <b>Version</b> 1.0.0
    </div>
    <strong>Copyright &copy; 2014-2018 <a href="https://adminlte.io">www.yunhu.com</a>.</strong> All rights
    reserved.
  </footer>
  <!-- Add the sidebar's background. This div must be placed
       immediately after the control sidebar -->
  <div></div>
</div>
<!-- ./wrapper -->
<script th:inline="javascript">

  var tablePackageMap = {

  };

  var selectTables = new Array();

  function ensurePackageName(tableName) {
    var pkgName = $("#"+tableName+"_id").val();
    tablePackageMap[tableName] = pkgName;
    console.log(tablePackageMap);
  }
  
  function generate() {
      var disk = $("#disk").val();
      var destDir = $("#destDir").val();

      var destOutputDir = disk + destDir;
      var basePackageName = $("#basePackage").val();
      var projectName = $("#projectName").val();

      var params = {
          tablePackageMap: tablePackageMap,
          destOutputDir: destOutputDir,
          basePackageName: basePackageName,
          projectName: projectName,
          selectedTables : selectTables
      };

      $.ajax({
          type: 'POST',
          contentType: "application/json",
          url: [[${#request.getContextPath()}]] + "/gen",
          data: JSON.stringify(params),
          success: function (data) {
              alert(data);
          },
          dataType: "text"
      });
  }

  function generateAndDownload() {
      var disk = $("#disk").val();
      var destDir = $("#destDir").val();

      var destOutputDir = disk + destDir;
      var basePackageName = $("#basePackage").val();
      var projectName = $("#projectName").val();

      var params = {
          tablePackageMap: tablePackageMap,
          destOutputDir: destOutputDir,
          basePackageName: basePackageName,
          projectName: projectName,
          selectedTables : selectTables
      };

      $("#reqStr").val(JSON.stringify(params));
      $("#theForm").submit();
  }

  // CheckBox选择处理,选择和反选
  function doSelect(tableName) {
      var obj = $("#" + tableName + "_cid");

      if(!obj.is(":checked")) {
          obj.prop("checked", false);
          var index = selectTables.indexOf(tableName);
          if(index != -1) selectTables.splice(index, 1);
      } else {
          obj.prop("checked", true);
          if(unique(tableName)) selectTables.push(tableName);
          console.log(selectTables);
      }
  }

  $(document).ready(function () {

      // CheckBox状态变更事件
      $("#checkbox").change(function () {
          if($("#checkbox").is(":checked")) {
              //$("input[name='tableName']").attr("checked", "true");
              $("input[name='tableName']").each(function () {
                  $(this).prop("checked", true);
                  // console.log($(this).attr("data-value"));
                  var tableName = $(this).attr("data-value");
                  if(unique(tableName)) selectTables.push(tableName);
                  console.log(selectTables);
              });
          } else {
              $("input[name='tableName']").prop("checked", false);
              selectTables.splice(0, selectTables.length);
          }
      });
  });
  
  function unique(element) {
      var index = selectTables.indexOf(element);
      if(index == -1)
          return true;
      return false;
  }

</script>
</body>
</html>
